package com.google.code.p.ivans.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

public class Order implements Serializable {
	
	private static final long serialVersionUID = -6279119744314801884L;

	private static final transient int MAX_TABLES = 20;
	
	private static long counter = 1;
	
	private long orderID;
	private int numberOfTable;
	private List<Integer> dishes;
	private GregorianCalendar orderDate;		
	
	
	public Order() {}
	
	public Order(int number, List<Integer> dishes, GregorianCalendar orderDate) {
		
		if(number < 1 || number > MAX_TABLES)
			throw new IllegalArgumentException("Number of table is out of bounds");
		for(Integer dish : dishes )
			if(dish == null)
				throw new IllegalArgumentException("Some dish is null or empty");
		
		//���� ���� ������ ��������
		if(orderDate.after(new GregorianCalendar()))
			throw new IllegalArgumentException("OrderDate after nowaday");
		
		this.orderID = counter++;
		this.numberOfTable = number;
		this.dishes = dishes;
		this.orderDate = orderDate;
	}
	
	public Order(long id, int number, List<Integer> dishes, GregorianCalendar orderDate) {
		
		if(number < 1 || number > MAX_TABLES)
			throw new IllegalArgumentException("Number of table is out of bounds");
		for(Integer dish : dishes )
			if(dish == null)
				throw new IllegalArgumentException("Some dish is null or empty");
		
		//���� ���� ������ ��������
		if(orderDate.after(new GregorianCalendar()))
			throw new IllegalArgumentException("OrderDate after nowaday");
		
		this.orderID = id;
		this.numberOfTable = number;
		this.dishes = dishes;
		this.orderDate = orderDate;
	}
	
	//get orderID
	public long getOrderID() {
		return orderID;
	}
	
	//get/set numberOfTable
	public int getNumberOfTable() {
		return numberOfTable;
	}	
	
	public void setNumberOfTable(int numberOfTable) {
		this.numberOfTable = numberOfTable;
	}

	//get/set dishes
	public List<Integer> getDishes() {
		return dishes;
	}

	public void setDishes(Integer[] dishes) {
		this.dishes = new ArrayList<Integer>(Arrays.asList(dishes));
	}
	
	public void addDishes(Integer dish) {
		this.dishes.add(dish);
	}

	//get/set orderDate
	public GregorianCalendar getOrderDate() {
		return orderDate;
	}
	
	public String getDateString() {
		return orderDate.get(Calendar.DAY_OF_MONTH)   + "-" + orderDate.get(Calendar.MONTH) + "-" + orderDate.get(Calendar.YEAR);
	}

	public void setOrderDate(GregorianCalendar orderDate) {
		this.orderDate = orderDate;
	}
	
	public String toString() {
		
		return "ID: " + orderID +
				"\n������: " + numberOfTable + 
				"\n�����: " + Arrays.toString(dishes.toArray()) + 
				"\n���� : " + (orderDate.get(Calendar.DAY_OF_MONTH)) + "-" + (orderDate.get(Calendar.MONTH) + 1) + "-" + orderDate.get(Calendar.YEAR);				
	}
	
	public static long getCounter() {
		return counter;
	}
	
	public static void setCounter(long newCounter) {
		counter = newCounter;
	}
}
